#include "ScrollItemList.h"
#include <sys/types.h>
#include <dirent.h>
#include <errno.h>
#include <vector>
#include <string>
#include <iostream>

ScrollItemList::ScrollItemList(std::string scenariosListXMLPath, int posX, int posY, Widget *parent)
	:ItemList(posX, posY, parent){

	this->pos = 0;
	this->cantItems = 0;
	this->keySelected = "";

	//Path to scenario
	std::string scenariosPath = XML_PATH;
	std::string scenarioName = "";

	DIR *dp;
	struct dirent *dirp;
	if((dp = opendir(scenariosPath.c_str())) != NULL) {
		while ((dirp = readdir(dp)) != NULL) {
			scenarioName = dirp->d_name;
			if(scenarioName.size() > 4){
				if(scenarioName.substr(scenarioName.size()-4, 4) == ".xml"){
					scenarioName = scenarioName.substr(0,scenarioName.size()-4);
					if(this->cantItems==0){
						listItems.push_back(new Item(scenarioName,true));
						this->keySelected = scenarioName;
					}else{
						listItems.push_back(new Item(scenarioName));
					}
					this->cantItems++;
				}
			}
		}
		closedir(dp);
	}

	if(this->cantItems > LIST_ITEMS_ON_SCREEN){
		this->bar_pos_height = (LIST_HEIGHT-2*ARROW_HEIGHT-BAR_HEIGHT) / (this->cantItems - LIST_ITEMS_ON_SCREEN);
	}else{
		this->bar_pos_height = (LIST_HEIGHT-2*ARROW_HEIGHT-BAR_HEIGHT);
	}

	std::string upImg = UP_IMG;
	std::string downImg = DOWN_IMG;
	std::string barImg = BAR_IMG;

	this->upButton = new ButtonListArrow(upImg,LIST_WIDTH-ARROW_WIDTH,0,this, new ItemListUpButtonListener());
	this->downButton = new ButtonListArrow(downImg,LIST_WIDTH-ARROW_WIDTH,LIST_HEIGHT-ARROW_HEIGHT,this, new ItemListDownButtonListener);
	this->barButton = new Button(" ",barImg,LIST_WIDTH-ARROW_WIDTH+1,ARROW_HEIGHT,this,NULL);

	fillButtonList();

	this->surface =	SurfaceUtils::colorSurface(LIST_WIDTH,LIST_HEIGHT,Color::BLANCO);

}

void ScrollItemList::handleEvent(Event event) {

	if ((event.getType()) == ItemList_UpButton) {
		if (this->pos > 0){
			pos--;
			fillButtonList();
		}
	} else if ((event.getType()) == ItemList_DownButton) {
		if (this->pos+1 <= this->cantItems-LIST_ITEMS_ON_SCREEN){
			pos++;
			fillButtonList();
		}
	}

	//if (!isMouseOver(event))
		//return;

	if ((event.getType()) == MouseButtonPressed) {
		std::list<ButtonListItem*>::iterator it;
		for (it = buttonList.begin(); it != buttonList.end(); ++it) {
			if(((ButtonListItem*)*it)->isMouseOver(event,parcel)){
				std::list<ButtonListItem*>::iterator it2;
				for (it2 = buttonList.begin(); it2 != buttonList.end(); ++it2) {
					if(((ButtonListItem*)*it2)->isMouseOver(event,parcel)){
						((ButtonListItem*)*it2)->setPressed(true);
						this->keySelected = ((ButtonListItem*)*it2)->getText();
					}else{
						((ButtonListItem*)*it2)->setPressed(false);
					}
				}
				std::list<Item*>::iterator it3;
				for (it3 = listItems.begin(); it3 != listItems.end(); ++it3) {
					if(this->keySelected == (*it3)->name){
						(*it3)->selected = true;
					}else{
						(*it3)->selected = false;
					}
				}
			}
		}
		Logger::trace("ScrollItemList", "handleEvent", "MOUSEBUTTONDOWN");
	} else if ((event.getType()) == MouseButtonReleased) {
		Logger::trace("ScrollItemList", "handleEvent", "SDL_MOUSEBUTTONUP");
	}

	this->upButton->handleEvent(event,this->parcel);
	this->downButton->handleEvent(event,this->parcel);

	// falta manejar la barra
	//this->barButton->handleEvent(event,this->parcel);

	Widget::handleEvent(event);
}

void ScrollItemList::resolve() {
	this->upButton->resolve();
	this->downButton->resolve();

	// falta manejar la barra
	//this->barButton->resolve();

	//Dibujo botones visibles en la lista
	std::list<ButtonListItem*>::iterator it;
	for (it = buttonList.begin(); it != buttonList.end(); ++it) {
		((ButtonListItem*)*it)->resolve();
	}

	Widget::resolve();
}

void ScrollItemList::showOn(SDL_Surface* surface) {

	SDL_FreeSurface(this->surface);
	this->surface =	SurfaceUtils::colorSurface(LIST_WIDTH,LIST_HEIGHT,Color::BLANCO);

	this->upButton->showOn(this->surface);
	this->downButton->showOn(this->surface);
	this->barButton->showOn(this->surface);

	//Dibujo botones visibles en la lista
	std::list<ButtonListItem*>::iterator it;
	for (it = buttonList.begin(); it != buttonList.end(); ++it) {
		((ButtonListItem*)*it)->showOn(this->surface);
	}

	Widget::showOn(surface);
}

void ScrollItemList::fillButtonList(){
	std::list<Item*>::iterator it;
	int tmpPos = 0;
	int tmpX = 0;
	int tmpY = 0;

	while (!buttonList.empty()) {
		delete buttonList.back();
		buttonList.pop_back();
	}

	it = listItems.begin();
	while ( it != listItems.end() && tmpPos<pos+LIST_ITEMS_ON_SCREEN) {
		if(tmpPos>=pos){
			ButtonListItem* tmpButton = new ButtonListItem((*it)->name,Color::AZUL, (*it)->selected,ITEM_WIDTH, ITEM_HEIGHT, tmpX, tmpY, this);
			buttonList.push_back(tmpButton);
			tmpY = tmpY + ITEM_HEIGHT;
		}
		tmpPos++;
		it++;
	}

	// Recreo la barra
	delete this->barButton;
	std::string barImg = BAR_IMG;
	this->barButton = new Button(" ",barImg,LIST_WIDTH-ARROW_WIDTH+1,ARROW_HEIGHT+pos*bar_pos_height,this,NULL);
}

std::string ScrollItemList::getSelected(){
	return keySelected;
}

ScrollItemList::~ScrollItemList(){
	delete upButton;
	delete downButton;
	delete barButton;
	buttonList.clear();
}




